﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QDebug>
#include <QButtonGroup>
#include "dataFormate.h"
#include "mainwindowpagecontrol.h"
#include "userdataformate.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_Calibration(NULL)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint);
    QButtonGroup * box = new QButtonGroup;
    // 设置是否互斥
    box->setExclusive(true);
    // 将需要互斥的按钮全部添加到 QButtonGroup 中
    box->addButton(ui->user_Btn);
    box->addButton(ui->evaluate_Btn);
    box->addButton(ui->train_Btn);
    box->addButton(ui->record_Btn);
    box->addButton(ui->set_Btn);
    //切换页面
    connect(MainWindowPageControl::getInstance(),SIGNAL(signalSwitchPage(int)),this,SLOT(slotSwitchPage(int)));
    connect(this,SIGNAL(signalsData(ST_RealParam)),ui->evaluate_page,SLOT(slotReceiveData(ST_RealParam)));
    connect(this,SIGNAL(signalsData(ST_RealParam)),ui->training_page,SLOT(slotReceiveData(ST_RealParam)));
    connect(CurrentUserData::getInstace(),SIGNAL(signalUserChanged()),this,SLOT(slotCurrentUserChanged()));

    ui->cell_label->setScaledContents(true);

    m_Calibration = new Calibration();
    connect(m_Calibration,SIGNAL(sendRealParam(ST_RealParam)),this,SLOT(slotReceiveData(ST_RealParam)));

    m_Calibration->raise();
}
//接收数据
void MainWindow::setBatteryPercent(int power)
{
    if(power <= 20)
        ui->cell_label->setPixmap(QPixmap(":/icon/cell20%.png"));
    else if(power <=40)
        ui->cell_label->setPixmap(QPixmap(":/icon/cell40%.png"));
    else if(power <=60)
        ui->cell_label->setPixmap(QPixmap(":/icon/cell60%.png"));
    else if(power <=80)
        ui->cell_label->setPixmap(QPixmap(":/icon/cell80%.png"));
    else if(power <=100)
        ui->cell_label->setPixmap(QPixmap(":/icon/cell100%.png"));
}

void MainWindow::slotSwitchPage(int index)
{
    switch(index)
    {
    case 0: //UserPage_E
        ui->stackedWidget->setCurrentIndex(UserPage_E);
        break;
    case 1: //TrainPage_E
        ui->stackedWidget->setCurrentIndex(EvaluatePage_E);
        break;
    case 2: //EvaluatePage_E
        ui->stackedWidget->setCurrentIndex(TrainPage_E);
        break;
    case 3: //RecordPage_E
        ui->stackedWidget->setCurrentIndex(RecordPage_E);
        break;
    case 4: //SettingPage_E
        ui->stackedWidget->setCurrentIndex(SettingPage_E);
        break;
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_user_Btn_clicked()
{
    MainWindowPageControl::getInstance()->setCurrentPage(UserPage_E);
}

void MainWindow::on_evaluate_Btn_clicked()
{
    MainWindowPageControl::getInstance()->setCurrentPage(EvaluatePage_E);
}

void MainWindow::on_train_Btn_clicked()
{
    MainWindowPageControl::getInstance()->setCurrentPage(TrainPage_E);
}

void MainWindow::on_record_Btn_clicked()
{
    MainWindowPageControl::getInstance()->setCurrentPage(RecordPage_E);
}

void MainWindow::on_set_Btn_clicked()
{

    MainWindowPageControl::getInstance()->setCurrentPage(SettingPage_E);
}

void MainWindow::setUser(const ST_PatientMsg &st_patientMsg)
{
    ui->userName_label->setText(st_patientMsg.userName);
}

void MainWindow::showEvent(QShowEvent *event)
{
    Q_UNUSED(event)
    QTimer::singleShot(1000,this,[=]{
         m_Calibration->show();
    });

}

void MainWindow::on_quit_Btn_clicked()
{
    this->close();
}

void MainWindow::slotCurrentUserChanged()
{
    setCurrentUser();
}

void MainWindow::slotReceiveData(ST_RealParam st_realParam)
{
    emit signalsData(st_realParam);
    setBatteryPercent(st_realParam.battery);
}

void MainWindow::setCurrentUser()
{
    ST_PatientMsg st_PatientMsg = CurrentUserData::getInstace()->getCurrentPatientMsg();
    ui->userName_label->setText(QString("%1").arg(st_PatientMsg.userName));
}

void MainWindow::changeEvent(QEvent* event)
{
    switch (event->type())
    {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        QWidget::changeEvent(event);
        break;
    }
}
